package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.dto.DefectiveProductDto;
import com.weixing.mall.provider.model.dto.DefectiveProductSaveDto;
import com.weixing.mall.provider.model.excel.DefectiveProductED;
import com.weixing.mall.provider.model.query.DefectiveProductQuery;
import com.weixing.mall.provider.model.vo.DefectiveProductVo;
import com.weixing.mall.provider.service.IDefectiveProductService;
import com.weixing.mall.provider.support.excaldata.StockCodeData;
import com.weixing.mall.provider.support.excalservice.DefectiveStockCodeDataListener;
import com.weixing.mall.provider.support.excalservice.DepotExcalService;
import com.weixing.mall.provider.support.excalservice.StockCodeDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Author wx
 * @Date 2019/10/25 10:51
 * @Version 1.0
 */
@RestController
@RequestMapping("defectiveProduct")
@Api(value = "次品库管理Api",tags = "次品库管理")
public class DefectiveProductController extends BaseController {
    @Autowired
    private IDefectiveProductService defectiveProductService;


    @ApiOperation(value = "列表查询")
    @GetMapping("")
    public Result selectList(DefectiveProductQuery defectiveProductQuery){
        return ResultUtil.success(defectiveProductService.selectListOfPage(initMpPage(),defectiveProductQuery));
    }
    @ApiOperation(value = "次品库管理列表导出Excel")
    @GetMapping("exportExcel")
    @SysLog(type = OperateEnum.E,desp = "次品库管理列表导出")
    public void downLong(DefectiveProductQuery defectiveProductQuery, HttpServletResponse response) {
        IPage<DefectiveProductVo> list = defectiveProductService.selectListOfPage(initMpPage().setSize(-1), defectiveProductQuery);
        try {
            String fileName = "次品库管理列表";
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            if(StrUtil.isEmpty(fileName)){
                fileName = DateUtil.now();
            }
            String fileName2 = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName2 + ".xlsx");
            EasyExcel.write(response.getOutputStream(), DefectiveProductED.class).sheet(fileName).doWrite(DepotExcalService.loadDefectiveProductList(list.getRecords()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "修改")
    @PostMapping("updated")
    public Result updated(DefectiveProductDto defectiveProductDto){
        defectiveProductService.updated(defectiveProductDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "入次品库")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(DefectiveProductSaveDto defectiveProductSaveDto,@SysInfo SysAuth auth){
        defectiveProductService.create(defectiveProductSaveDto,auth);
        return ResultUtil.success();
    }
    @ApiOperation(value = "二期商品入次品库")
    @PostMapping("saveByGoods")
    @NoRepeatSubmit
    public Result saveByGoods(DefectiveProductSaveDto defectiveProductSaveDto,@SysInfo SysAuth auth){
        defectiveProductService.create(defectiveProductSaveDto,auth);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据id查找次品库详情")
    @GetMapping("selectDefectiveProductById/{id}")
    public Result selectDefectiveProductById(@PathVariable Long id){
        return ResultUtil.success(defectiveProductService.selectDefectiveProductById(id));
    }

    /**
     * 次品库库存编码导入
     * @param file
     * @return
     */
    @ApiOperation("次品库库存编码导入")
    @PostMapping(value = "excelImprotstockSn")
    public Result initStockCode( MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), StockCodeData.class, new DefectiveStockCodeDataListener(null, defectiveProductService, null)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.error("Excel导入失败");
        }
        return ResultUtil.success();
    }
}
